package com.zlm.huanxin.util;

import com.alibaba.fastjson.JSONObject;
import com.zlm.amqp.service.Sender;
import com.zlm.common.constants.CodeConstants;
import com.zlm.huanxin.constants.HuanxinConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.util.GsonUtils;
import com.zlm.common.util.HttpClientUtils;
import com.zlm.common.util.PasswordUtils;
import com.zlm.huanxin.entity.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 环信工具类
 * @Author: duyufu
 * @Date: 2019/7/2 10:16
 */
@Component
public class HuanxinUtils {

    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;
    private static RedisTemplate<String, Serializable> redisCache;

   @Autowired
   private Sender sender;
   private static Sender rabbitTemplate;


    @PostConstruct
    public void init() {
        redisCache = this.redisCacheTemplate;
        rabbitTemplate = this.sender;
    }

    /**
     * 获取管理员权限，用于授权操作
     * @return
     */
    public static String getAdminToken(){
        String url = String.format(HuanxinConstants.HX_GET_TOKEN, HuanxinConstants.ORG_NAME, HuanxinConstants.APP_NAME);
        String message =
                "{'grant_type':'"+HuanxinConstants.GRANT_TYPE+"','client_id':'"+HuanxinConstants.CLIENT_ID+"','client_secret':'"+HuanxinConstants.CLIENT_SECRET+"'}";
        HashMap<String, Object> args = HttpClientUtils.doPost(url, JSONObject.parseObject(message), null);
        String code = (String)args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            JSONObject response = JSONObject.parseObject(args.get("response").toString());
            String accessToken = response.getString("access_token");
            redisCache.opsForValue().set("accessToken",accessToken);
            return accessToken;
        }
        return "";
    }
    
    /**
     * 环信注册
     * @param userId
     * @return
     * @throws Exception
     */
    public static Map<String,Object> huanxinRegister(Integer userId,String username,String queueName) {
        String url = String.format(HuanxinConstants.HX_USER_REG, HuanxinConstants.ORG_NAME, HuanxinConstants.APP_NAME);
        String password = PasswordUtils.getPassword(username);
        System.out.println("环信密码：" + password);
        String message =
                "{'username':'"+username+"','password':'"+password+"','nickname':'"+username+"'}";
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPost(url,JSONObject.parseObject(message),accessToken);
        Map<String, Object> map = getEntities(args);
        if ((int) map.get("code") == HttpStatus.SC_OK) {
            //说明环信注册成功，将username、密码发送到rabbitmq队列中，通过后台接收数据更新Sys_Emp表并修改环信注册状态
            String context = String.valueOf(userId).concat("&&").concat(username).concat("&&").concat(password);
            rabbitTemplate.hxRegisterSend(context,queueName);
        }
        return map;
    }

    /**
     * 获取单个用户
     * @param username
     * @return
     */
    public static Map<String,Object> huanxinGetUser(String username) {
        String url = String.format(HuanxinConstants.HX_GET_USER, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, username);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        return getEntities(args);
    }

    /**
     * 添加好友
     * @param ownerUsername
     * @param friendUsername
     * @return
     */
    public static Map<String,Object> addFriend(String ownerUsername, String friendUsername) {
        String url = String.format(HuanxinConstants.HX_ADD_FRIEND, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, ownerUsername, friendUsername);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPost(url, null, accessToken);
        return getEntities(args);
    }

    private static Map<String, Object> getEntities(HashMap<String, Object> args) {
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HuanxinResponse response = (HuanxinResponse) GsonUtils.JSONToObject(result, HuanxinResponse.class);
            List<UserResponse> entities = response.getEntities();
            if (entities.size() == NumberConstants.NUM_1) {
                map.put("entities", entities.get(0));
            }
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 删除好友
     * @param ownerUsername
     * @param friendUsername
     * @return
     */
    public static Map<String,Object> deleteFriend(String ownerUsername, String friendUsername) {
        String url = String.format(HuanxinConstants.HX_DELETE_FRIEND, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, ownerUsername, friendUsername);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url, accessToken);
        return getEntities(args);
    }

    /**
     * 获取好友列表
     * @param ownerUsername
     * @return
     */
    public static Map<String,Object> getFriendList(String ownerUsername) {
        String url = String.format(HuanxinConstants.HX_GET_FRIEND_LIST, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, ownerUsername);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        Map<String, Object> map = new HashMap<>();
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HuanxinResponse response = (HuanxinResponse) GsonUtils.JSONToObject(result, HuanxinResponse.class);
            List<Object> friendList = response.getData();
            map.put("friendList", friendList);
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 强制下线
     * @param username
     * @return
     */
    public static Map<String,Object> disconnect(String username) {
        String url = String.format(HuanxinConstants.HX_DISCONNECT, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, username);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        Map<String, Object> map = new HashMap<>();
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {


        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 获取一个用户参与的所有群组
     * @param username
     * @return
     */
    public static Map<String,Object> getAllJoinedGroup(String username) {
        String url = String.format(HuanxinConstants.HX_GET_JOINED_GROUP, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME, username);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HuanxinResponse response = (HuanxinResponse) GsonUtils.JSONToObject(result, HuanxinResponse.class);
            List<Object> groupList = response.getData();
            map.put("groupList", groupList);
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 创建群组
     * @param json
     * @return
     */
    public static Map<String,Object> createGroup(JSONObject json) {
        String url = String.format(HuanxinConstants.HX_CREATE_GROUP, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPost(url, json, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HxGroupResponse response = (HxGroupResponse) GsonUtils.JSONToObject(result, HxGroupResponse.class);
            GroupResponse data = response.getData();
            map.put("groupid",data.getGroupid());
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 获取群组详情
     * @param groupHxId
     * @return
     */
    public static Map<String,Object> getGroupDetail(String groupHxId) {
        String url = String.format(HuanxinConstants.HX_GET_GROUP_DETAIL, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HxGroupDetailResponse hxGroupDetail = (HxGroupDetailResponse) GsonUtils.JSONToObject(result,
                    HxGroupDetailResponse.class);
            map.put("HxGroupDetail",hxGroupDetail);
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 修改环信群组信息
     * @param groupHxId
     * @param groupname
     * @param description
     * @param maxusers
     * @return
     */
    public static Map<String,Object> updateHxGroup(String groupHxId,String groupname,String description,Integer maxusers) {
        String url = String.format(HuanxinConstants.HX_UPDATE_GROUP, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        String message =
                "{'groupname':'"+groupname+"','description':'"+description+"','maxusers':'"+maxusers+"'}";
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPut(url,JSONObject.parseObject(message),accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            //说明更新成功

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 解散群组
     * @param groupHxId
     * @return
     */
    public static Map<String,Object> deleteHxGroup(String groupHxId) {
        String url = String.format(HuanxinConstants.HX_DELETE_GROUP, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            //说明更新成功

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 删除单个群组成员
     * @param groupHxId
     * @param username
     * @return
     */
    public static Map<String,Object> deleteSingleHxGroupMember(String groupHxId,String username) {
        String url = String.format(HuanxinConstants.HX_DELETE_SINGLE_GROUP_MEMBER, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId,username);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HxGroupDetailResponse hxGroupDetail = (HxGroupDetailResponse) GsonUtils.JSONToObject(result,
                    HxGroupDetailResponse.class);
            map.put("HxGroupDetail",hxGroupDetail);
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 分页获取群成员
     * @param groupHxId
     * @param pagenum
     * @param pagesize
     * @return
     */
    public static Map<String,Object> getHxGroupMember(String groupHxId,Integer pagenum,Integer pagesize) {
        String url = String.format(HuanxinConstants.HX_GET_GROUP_MEMBER, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId,pagenum,pagesize);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            //说明更新成功
            String result = args.get("result").toString();
            HxGroupResponse hxGroup = (HxGroupResponse) GsonUtils.JSONToObject(result,
                    HxGroupResponse.class);
            map.put("hxGroupMember",hxGroup.getData());
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 批量添加群组成员
     * @param groupHxId
     * @param json
     * @return
     */
    public static Map<String,Object> addHxGroupMember(String groupHxId,JSONObject json) {
        String url = String.format(HuanxinConstants.HX_ADD_GROUP_MEMBER, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPost(url, json, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 批量删除群组成员
     * @param groupHxId
     * @param usernames
     * @return
     */
    public static Map<String,Object> deleteHxGroupMember(String groupHxId,String usernames) {
        String url = String.format(HuanxinConstants.HX_DELETE_GROUP_MEMBER, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId,usernames);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 获取管理员列表
     * @param groupHxId
     * @return
     */
    public static Map<String,Object> getHxGroupAdmin(String groupHxId) {
        String url = String.format(HuanxinConstants.HX_GET_GROUP_ADMIN, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doGet(url, accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            String result = args.get("result").toString();
            HxGroupAdminResponse hxGroup = (HxGroupAdminResponse) GsonUtils.JSONToObject(result,
                    HxGroupAdminResponse.class);
            map.put("adminList",hxGroup.getData());
        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 添加群组管理员
     * @param groupHxId
     * @param username
     * @return
     */
    public static Map<String,Object> addHxGroupAdmin(String groupHxId,String username) {
        String url = String.format(HuanxinConstants.HX_ADD_GROUP_ADMIN, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        String message = "{'newadmin':'"+username+"'}";
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPost(url,JSONObject.parseObject(message),accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 移除环信管理员
     * @param groupHxId
     * @param username
     * @return
     */
    public static Map<String,Object> deleteHxGroupAdmin(String groupHxId,String username) {
        String url = String.format(HuanxinConstants.HX_DELETE_GROUP_ADMIN, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId,username);
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doDelete(url,accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }

    /**
     * 转让群组
     * @param groupHxId
     * @param username
     * @return
     */
    public static Map<String,Object> transferHxGroup(String groupHxId,String username) {
        String url = String.format(HuanxinConstants.HX_TRANSFER_GROUP, HuanxinConstants.ORG_NAME,
                HuanxinConstants.APP_NAME,groupHxId);
        String message = "{'newowner':'"+username+"'}";
        //从redis中获取accessToken值
        String accessToken = (String)redisCache.opsForValue().get("accessToken");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = getAdminToken();
        }
        HashMap<String, Object> args = HttpClientUtils.doPut(url,JSONObject.parseObject(message),accessToken);
        Map<String, Object> map = new HashMap<>(2);
        String code = (String) args.get("code");
        if (Integer.valueOf(code).intValue() == HttpStatus.SC_UNAUTHORIZED) {
            getAdminToken();
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        } else if (Integer.valueOf(code).intValue() == HttpStatus.SC_OK) {
            //说明更新成功

        }else {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        map.put("code", Integer.valueOf(code).intValue());
        return map;
    }
}
